import axios from 'axios'
import { createDiscreteApi } from 'naive-ui'

const { message } = createDiscreteApi(['message'])
window.$message = message

const service = axios.create({
  baseURL: import.meta.env['RENDERER_VITE_BASE_URL'], // url = base url + request url
  timeout: 5000 // request timeout
})

// 请求拦截
service.interceptors.request.use(
  (config) => {
    config.headers['isapp'] = true
    return config
  },
  (error) => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// 响应拦截
service.interceptors.response.use(
  (response) => {
    const { data } = response
    if (data.code == 30000) {
      return data.data
    } else {
      return null
    }
  },
  (error) => {
    window.$message.error(error.response.data.message)
    return Promise.reject(error)
  }
)

export default service
